package models

import (
	"gorm.io/gorm"
	"time"
)

// Message 聊天消息模型
type Message struct {
	gorm.Model
	RoomID   uint      `json:"room_id" gorm:"not null"`
	UserID   int       `json:"user_id" gorm:"not null"`
	UserName string    `json:"user_name" gorm:"size:50"`
	Content  string    `json:"content" gorm:"type:text"`
	Time     time.Time `json:"time"`
}

func CreateMessage(msg *Message) error {
	return GetDB().Create(msg).Error
}

func GetMessagesByRoom(roomID uint, limit int) ([]Message, error) {
	var msgs []Message
	err := GetDB().Where("room_id = ?", roomID).Order("created_at desc").Limit(limit).Find(&msgs).Error
	return msgs, err
}
